home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_fresnelplastic.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.2 KB  |  113 lines

  1. /* 
  2.  * TLFresnelPlastic.sl -- simple shader illustrating fresnel().
  3.  *
  4.  * DESCRIPTION:
  5.  *    Simple Fresnel shader using plastic illuminance.
  6.  *
  7.  * PARAMETERS
  8.  *
  9.  * AUTHOR: Tal Lancaster  tal@SpamSucks_renderman.org
  10.  *
  11.  * HISTORY:
  12.  *    Jan 26 2002  Created
  13.  */
  14.  
  15. /* From Advanced RenderMan */
  16. #include "k3d_reflections.h"
  17.  
  18. #ifdef PRMAN  /* as of prman10 */
  19. #define CHKTX(tx) ((tx != "" && textureinfo (tx, "exists", 0) != 0)? 1: 0)
  20. #else
  21. #define CHKTX(tx) \
  22.     ((tx != "")? 1: 0)
  23. #endif
  24.  
  25. surface k3d_fresnelplastic (
  26.    color Csurf = 0.5; /* cat Color 
  27.              desc {Base color for surface if 
  28.              not over-written by "colorMap } */
  29.    string colorMap = ""; /* cat Color
  30.                 desc {Optional color map.  Replaces 'Csurf'.} */
  31.    
  32.    float ior = 0;
  33.    /* desc { Index of Refraction.  Used to get Fresnel falloff.
  34.       0: turns off fresnel and gives plastic look. Typical values
  35.       gt 1.0 and lt 2.2}  range {1.01 2.2 .01} */
  36.    float Ka= 1;       /* desc { Usual } */
  37.    float Kd= 0.5;     /* desc { Usual } */
  38.    float Ks= 0.5;     /* desc { Usual } */
  39.    float roughness= 0.1;  /* desc {Specular roughness} */
  40.    color Cspec = 1;       /* desc {Specular color} */
  41.    float Kr = 0;  /* cat Reflection desc {Strength of reflection } */
  42.    string reflectMap = ""; 
  43.    /* cat Reflection desc {Name of reflection map. } type texture */
  44.    float reflectBlur = 0; 
  45.    /* cat Reflection desc {Percentage amount to blur map} */
  46.    string reflectSpace = "world";  /* cat Reflection 
  47.    desc {Space the reflection calculations are performed in. } */
  48.    float reflectDist = 1e10;
  49.    /* cat Reflection desc {The size of the room to base the reflection
  50.       lookup off of.} */
  51.  
  52.    float flipS = 0; /* cat ST type switch desc {Flip S on texture lookups.} */
  53.    float flipT = 0; /* cat ST type switch desc {Flip S on texture lookups.} */
  54.    float MtorFlip = 0; /* cat ST type switch def 1
  55.               desc {Swap S/T access to overcome Maya/MtoR
  56.               NURBs differences.} */
  57. )
  58. {
  59.     float ss, tt;
  60.     float fKt, fKr;
  61.     color Ct;
  62.  
  63.     normal Nf = normalize (faceforward( normalize(N), I ));
  64.     vector V = -normalize(I);
  65.     vector R = normalize (reflect (I, Nf));
  66.  
  67.     /* Parametric space adjustments */
  68.     if (MtorFlip == 1) {
  69.     ss = t;
  70.     tt = s;
  71.     }
  72.     else {
  73.     ss = s;
  74.     tt = t;
  75.     }
  76.     if (flipS == 1)
  77.     ss = 1 - ss;
  78.     if (flipT == 1)
  79.     tt = 1 - tt;
  80.  
  81.     /* Calculate fresnel index of refraction */
  82.     if (ior != 0) {
  83.       fresnel (normalize (I), Nf, (I.Nf > 0)? ior: 1/ior,
  84.            fKr, fKt);
  85.       /* hack */
  86.       fKt = 1 - fKr;
  87.     }
  88.     else {
  89.       /* fresnel turned off */
  90.       fKr = fKt = 1;
  91.     }
  92.  
  93.     /* base color */
  94.     /*Ct = (CHKTX (colorMap))? texture (colorMap, ss, tt): Csurf; */
  95.     if (CHKTX (colorMap) == 1)
  96.       Ct = texture (colorMap, ss, tt);
  97.     else
  98.       Ct = Csurf;
  99.  
  100.     /* Environment */
  101.     color Cr = 0;
  102.     float reflOpacity = 1;
  103.     if (reflectMap != "")
  104.       Cr = Environment (reflectMap, reflectSpace, reflectDist, 
  105.             P, R, reflectBlur, reflOpacity);
  106.  
  107.     /* lighting */
  108.     Oi = Os;
  109.     Ci = Os * ( fKt * Ct * (Ka*ambient() + Kd*diffuse(Nf)) +
  110.                 fKr * Cspec * Ks * specular(Nf,V,roughness) +
  111.         Kr * Cr * fKr);
  112. }
  113.